var app = getApp();
Page({
	data: {
		cdnUrl: '',
		isWxapp: true,
		userInfo: {
			id: 0,
			avatar: '/assets/images/avatar.png',
			nickname: '用户昵称',
			money: 0,
			score: 0,
			level: 0
		},
		/* 自定义导航栏参数 */
		loading: false,
		color: '#FFFFFF',
		background: '#FFFFFF',
		show: true,
		animated: false,
		back: true,
		title: "资金明细",
		
		/* 筛选 */
		status: 0,
		status_text: '全部',
		moneyList: [],
		array: [ '全部', '支出', '收入' ],
		cashMoneyShow: false, //提现弹窗
		/*体现方式选择*/
		radioItems: [
			{ name: '银行卡', value: '0', checked: false },
			{ name: '支付宝', value: '1', checked: false }
		],
		form: {
			cost: '',
			type: '',
			bank_account: '',
			bank_username: '',
			bank_name: '',
			ali_account: '',
			ali_username: ''
		}
	},
	/*全部提现*/
	allCost () {
		var that = this;
		that.setData({
			['form.cost']: that.data.userInfo.money
		})
	},
	onClosePopup () {
		var that = this;
		that.setData({
			cashMoneyShow: false
		})
	},
	onShowPopup () {
		var that = this;
		that.setData({
			cashMoneyShow: true
		})
	},
	formSubmit (e) {
		var that = this;
		console.log('form发生了submit事件，携带数据为：', e.detail.value)
		var data = e.detail.value;
		if ( parseInt(data.type)===0 ) {
			if ( !data.bank_account ) {
				app.error('请填写银行卡号');
				return false;
			}
			if ( !data.bank_username ) {
				app.error('请填写开户姓名');
				return false;
			}
			if ( !data.bank_name ) {
				app.error('请填写开户行');
				return false;
			}
		} else if ( parseInt(data.type)===1 ) {
			if ( !data.ali_account ) {
				app.error('请填写支付宝账号');
				return false;
			}
			if ( !data.ali_username ) {
				app.error('请填写支付宝用户名');
				return false;
			}
		} else {
			app.error('请选择提现方式');
			return false;
		}
		if ( data.cost*1>that.data.userInfo.money*1 ) {
			app.error('可提现最大金额为：' + that.data.userInfo.money);
			return false;
		} else if ( data.cost*1===0 ) {
			app.error('提现金额必须大于0');
			return false;
		}
		app.request('/addons/cms/wxapp.user/withdrawal', data, function (data, ret) {
			app.success(ret.msg);
			that.moneyList();
			that.userInfo();
			that.setData({
				cashMoneyShow: false
			});
		}, function (data, ret) {
			app.error('服务器错误');
		});
	},
	userInfo: function () {
		var that = this;
		app.request('/addons/cms/wxapp.user/userInfo', {}, function (data, ret) {
			that.setData({
				userInfo: data.userInfo
			});
		}, function (data, ret) {
			// app.error(ret.msg);
		});
	},
	/*提现方式选择*/
	radioChange: function (e) {
		var value = e.detail.value;
		this.setData({
			['form.type']: value
		})
	},
	moneyList: function (status) {
		var that = this;
		app.request('/addons/cms/wxapp.user/moneyList', { status: status }, function (data, ret) {
			that.setData({
				moneyList: data
			});
			if ( data.length==0 ) {
				app.error('暂无数据');
			}
		}, function (data, ret) {
			console.log(ret);
			app.error('服务器错误');
		});
	},
	onLoad: function () {
		var that = this;
		that.setData({
			cdnUrl: app.cdnUrl
		});
	},
	onShow: function (e) {
		var that = this;
		that.setData({
			cdnUrl: app.cdnUrl
		});
		that.moneyList(that.data.status);
		if ( app.globalData.userInfo ) {
			that.setData({
				userInfo: app.globalData.userInfo
			});
		} else {
			app.showLoginModal(e);
		}
	},
	
	bindPickerChange: function (e) {
		console.log(e);
		var that = this;
		var status = e.detail.value;
		that.setData({
			status: status,
			status_text: that.data.array[status]
		})
		that.moneyList(status);
	}
	
})
